import json
import time

import moment
import scrapy

__all__ = [
    'BaseItem',
    'scrapy'
]


class BaseItem(scrapy.Item):
    uuid = scrapy.Field()                           # 平台唯一ID
    type = scrapy.Field(type=str)                   # 类型
    title = scrapy.Field(type=str, maxLength=3000)  # 标题
    author = scrapy.Field(type=str)                 # 作者
    url = scrapy.Field(type=str)                    # 访问URL
    content = scrapy.Field(type=str)                # 内容
    socialContent = scrapy.Field(type=str)          # 社交内容 - 仅社交内容存在
    pubTime = scrapy.Field(type=int)                # 发布时间(时间戳13位)
    fetchTime = scrapy.Field(type=int)              # 抓取时间
    referer = scrapy.Field(type=str)                # Referer
    depth = scrapy.Field()                          # depth
    origin = scrapy.Field(serializer=json.dumps)    # 采集源信息
    porigin = scrapy.Field(serializer=json.dumps)   # 源采集源信息
    emotion = scrapy.Field()                        # 情感(数据回填需要)
    kernelSentences = scrapy.Field()                # 内核类型(数据回填需要)

    def __init__(self):
        super().__init__()
        self['fetchTime'] = int(time.time() * 1000)

    def __setitem__(self, key, value):
        if key in self.fields:
            value_type = self.fields[key].get('type', None)
            value_max_length = self.fields[key].get('maxLength', None)
            if value != None:
                if value_type and value_type != type(value):
                    raise KeyError('%s field type is not %s' %
                                   (key, str(value_type)))

                if value_max_length and len(str(value)) > value_max_length:
                    raise KeyError('%s field max length is longer to %s' %
                                   (key, str(value_max_length)))

            # 对发布时间特别处理
            if key == 'pubTime':
                next_year = moment.now().add(year=1).epoch() * 1000
                if value and value > next_year:
                    raise KeyError('pubTime field might be err')

            self._values[key] = value
        else:
            raise KeyError('%s does not support field: %s' %
                (self.__class__.__name__, key))

    @classmethod
    def validate_item(self):
        for key in self.fields:
            if self.fields[key].get('is_required', False):
                if (key in self) and (self[key] is not None):
                    pass
                else:
                    raise KeyError('%s field is required' % (key))

    @classmethod
    def es_to_item(self, source, Item):
        item = Item()
        for key in Item.fields.keys():
            if source.get(key):
                item[key] = source[key]
        return item


if __name__ == '__main__':
    for key in BaseItem.fields.keys():
        print(key)